from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QHeaderView, QAbstractItemView, QTableWidgetItem, QMessageBox

from opsql import rootOp


class MOrder():
    def __init__(self):
        self.ui = QUiLoader().load('view/订单管理.ui')
        self.inittable()
        self.initcombo()
        self.ui.searchBtn.clicked.connect(self.inittable)
        self.ui.addBtn.clicked.connect(self.add)
        self.ui.modifyBtn.clicked.connect(self.modify)
        self.ui.deleteBtn.clicked.connect(self.delete)

    def inittable(self):
        id = self.ui.idIn.text()
        cid = self.ui.cidIn.text()
        sid = self.ui.sidIn.text()
        ispay = self.ui.ispayIn.currentData()
        result = rootOp.searchorder(id, cid, sid, ispay)
        row = 0
        if result:
            row = len(result)
        self.ui.tableWidget.setRowCount(row)
        self.ui.tableWidget.setColumnCount(7)
        self.ui.tableWidget.setHorizontalHeaderLabels(['订单id','学生id','电脑id','是否支付','金额','开始时间','结束时间'])
        self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.ui.tableWidget.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
        self.ui.tableWidget.verticalHeader().setVisible(False)
        self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)

        for i in range(row):
            for j in range(7):
                data = QTableWidgetItem(str(result[i][j]))
                self.ui.tableWidget.setItem(i, j, data)

    def initcombo(self):
        self.ui.ispayIn.addItem("请选择图书等级", -1)
        self.ui.ispayIn.addItem("未支付", 0)
        self.ui.ispayIn.addItem("已支付", 1)
        self.ui.ispayShow.addItem("请选择图书等级", -1)
        self.ui.ispayShow.addItem("未支付", 0)
        self.ui.ispayShow.addItem("已支付", 1)

    def add(self):
        id = self.ui.idShow.text()
        cid = self.ui.cidShow.text()
        sid = self.ui.sidShow.text()
        ispay = self.ui.ispayShow.currentData()
        cost = self.ui.costShow.text()
        starttime = self.ui.starttime.text()
        endtime = self.ui.endtime.text()
        result = rootOp.addorder(id, sid, cid, ispay, cost, starttime, endtime)
        if result:
            QMessageBox.information(self.ui, '提示', '添加成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '添加失败')

    def modify(self):
        id = self.ui.idShow.text()
        cid = self.ui.cidShow.text()
        sid = self.ui.sidShow.text()
        ispay = self.ui.ispayShow.currentData()
        cost = self.ui.costShow.text()
        starttime = self.ui.starttime.text()
        endtime = self.ui.endtime.text()
        result = rootOp.modifyorder(id, sid, cid, ispay, cost, starttime, endtime)
        if result:
            QMessageBox.information(self.ui, '提示', '修改成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '修改失败')

    def delete(self):
        row = self.ui.tableWidget.currentRow()
        if row == -1:
            QMessageBox.warning(self.ui, '提示', '请选择订单')
            return
        id = self.ui.tableWidget.item(row, 0).text()
        result = rootOp.deleteorder(id)
        if result:
            QMessageBox.information(self.ui, '提示', '删除成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '删除失败')